package com.war.controller.warehousing;

import com.war.dao.warehousing.PurchaseDao;
import com.war.pojo.system.Supplier;
import com.war.pojo.system.User;
import com.war.pojo.system.Warehouse;
import com.war.pojo.warehousing.PurchaseInfo;
import com.war.pojo.warehousing.ReturnInfo;
import com.war.service.safeManage.UserService;
import com.war.service.warehousing.PurchaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/warehousing")
public class PurchaseController {

    @Autowired(required = false)
    PurchaseService purchaseService;

    //采购入库表单
    @RequestMapping("/purchaseList")
    public String list(PurchaseInfo purchaseInfo,ModelMap m){
        HashMap<String,Object> map = purchaseService.selectAll(purchaseInfo);
        //把数据传到前端
        m.put("info",map);
        //把用户的查询条件传回到前端
        m.put("preCondition",purchaseInfo.getCondition());
        m.put("start",purchaseInfo.getStartDate());
        m.put("end",purchaseInfo.getEndDate());
        return "warehousing/purchase";
    }

    //采购入库记录查看详情
    @RequestMapping("/purchaseDetail")
    public String purchaseDetail(PurchaseInfo purchaseInfo,ModelMap m, HttpServletRequest request){
        //获得对应的采购记录
        PurchaseInfo purInfo = purchaseService.selectById(purchaseInfo);
        //把数据传到前端
        m.addAttribute("purInfo",purInfo);
        return "warehousing/purchase-detail";
    }

    //修改采购入库记录
    @RequestMapping("/purchaseEdit")
    public String purchaseEdit(PurchaseInfo purchaseInfo,ModelMap m, HttpServletRequest request){
        //获得对应的采购记录
        PurchaseInfo purInfo = purchaseService.selectById(purchaseInfo);
        //把数据传到前端
        m.addAttribute("purInfo",purInfo);
        return "warehousing/purchase-edit";
    }

    //加载供应商名称
    @RequestMapping("/supplier")
    @ResponseBody
    public List<Supplier> selectSupplierName(){
        return purchaseService.selectSupplierName();
    }

    //加载供应商电话
    @RequestMapping("/getSupplierTel")
    @ResponseBody
    public String getSupplierTel(Supplier supplier){
        return purchaseService.getSupplierTel(supplier);
    }

    //加载仓库名称
    @RequestMapping("/storage")
    @ResponseBody
    public List<Warehouse> selectStorageName(){
        return purchaseService.selectStorageName();
    }

    //获取仓库的管理员名称
    @RequestMapping("/getStorageManager")
    @ResponseBody
    public String getStorageManager(Warehouse warehouse){
        return purchaseService.getStorageManager(warehouse);
    }

    //加载采购员名称
    @RequestMapping("/buyer")
    @ResponseBody
    public List<User> selectBuyerName(){
        return purchaseService.selectBuyerName();
    }

    // 处理修改的ajax请求
    @RequestMapping("/edit")
    @ResponseBody
    public HashMap<String,Object> edit(PurchaseInfo purchaseInfo){
        HashMap<String,Object> map = new HashMap<>();
        String result = purchaseService.editPurchase(purchaseInfo);
        map.put("info",result);
        return map;
    }

    // 处理删除的ajax请求
    @RequestMapping("/delPurchase")
    @ResponseBody
    public HashMap<String,Object> del(PurchaseInfo purchaseInfo){
        HashMap<String,Object> map = new HashMap<>();
        String result = purchaseService.delPurchase(purchaseInfo);
        map.put("info",result);
        return map;
    }

    //访问添加采购记录的方法
    @RequestMapping("/insertPurchase")
    public String insertPurchase( HttpServletRequest request){
        return "warehousing/purchase-add";
    }

    // 处理添加的ajax请求
    @RequestMapping("/insertPurchaseAjax")
    @ResponseBody
    public HashMap<String,Object> insertPurchaseAjax(PurchaseInfo purchaseInfo){
        HashMap<String,Object> map = new HashMap<>();
        String result = purchaseService.insertPurchase(purchaseInfo);
        map.put("info",result);
        return map;
    }

    //退货表单页面
    @RequestMapping("/purchaseReturn")
    public String purchaseReturn(PurchaseInfo purchaseInfo,ModelMap m, HttpServletRequest request){
        //获得对应的采购记录
        PurchaseInfo reInfo = purchaseService.selectById(purchaseInfo);
        //把数据传到前端
        m.addAttribute("reInfo",reInfo);
        return "warehousing/purchase-return";
    }

    //处理退货的ajax请求
    @RequestMapping("/purchaseReturnAjax")
    @ResponseBody
    public HashMap<String,Object> purchaseReturn(ReturnInfo returnInfo ){
        HashMap<String,Object> map = new HashMap<>();
        String result = purchaseService.purchaseReturn(returnInfo);
        map.put("info",result);
        return map;
    }

    //处理比较入库数量与仓库所能容纳的数量的ajax请求
    @RequestMapping("/storageNumNow")
    @ResponseBody
    public HashMap<String,Object> storageNumNow(PurchaseInfo purchaseInfo){
        HashMap<String,Object> map = new HashMap<>();
        int result = purchaseService.storageNumNow(purchaseInfo);
        map.put("info",result);
        return map;
    }

    
    //批量删除
    @RequestMapping(value="delAll",method = RequestMethod.POST)
    @ResponseBody
    public HashMap<String,Object> delAll(@RequestBody List<PurchaseInfo> purchaseInfo){
        HashMap<String,Object> map = new HashMap<>();
        String result = purchaseService.delAll(purchaseInfo);
        map.put("info",result);
        return map;
    }


}
